<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        canvas {
            background: #ccc
        }
    </style>
</head>
<body>
<canvas width="500" height="400" id="canvas">
    您的浏览器不支持canvas，请更新！
</canvas>
</body>
<script>
    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext("2d");
    ctx.lineWidth=20;
    /*（1）创建一个渐变对象*/
    var g=ctx.createLinearGradient(0,0,500,0)
    /*（2）设置颜色点*/
    g.addColorStop(0,"blue");
    g.addColorStop(0.5,"red");
    g.addColorStop(1,"green");
  /*  （3）应用渐变对象到填充或描边上*/
    ctx.strokeStyle=g
   /* （4）绘制图形文本*/
    ctx.fillRect(0,0,500,400);

</script>
</html>